/*****************************************************************************

File:   otkAssemblyItemUtils.java

Date          Build     Author        Version    Comments
01-Nov-12    P-20-16    dshah          $$1        Created

****************************************************************************/

/* 
   
Utilites for Assembly Item / Flexible Component operations

*/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcAssembly.*;
import com.ptc.wfc.wfcAssembly.*;
import com.ptc.wfc.wfcComponentFeat.*;
import com.ptc.pfc.pfcComponentFeat.*;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
 
//***********************************************************
    class otkAssemblyItemUtils 
    {
	
	
	
    public otkAssemblyItemUtils()
	{
	}
    
	//***********************************************************
	/* Use dimension Ids to append Assembly Item array 
	 */
	public static AssemblyItems OTKUtilPopulateAssemblyItemsDimensionArray(
									       AssemblyItems AsmItemArray,		
									       WAssembly WAsm,
									       Model ItemOwner, 
									       intseq DimIds, 
									       WComponentPath ItemCompPath,
									       BufferedWriter fp_out_asmitem)
	{	
	    try 
		{
		    for (int i=0 ; i<DimIds.getarraysize() ; i++)
			{
			    AssemblyItemInstructions ThisAsmInstr = 
				wfcComponentFeat.AssemblyItemInstructions_Create(ItemOwner, ModelItemType.ITEM_DIMENSION, DimIds.get(i) );
			    
			    if (ItemCompPath != null)
				ThisAsmInstr.SetItemCompPath(ItemCompPath);
			    
			    AssemblyItem ThisAsmItem = WAsm.CreateAssemblyItem(ThisAsmInstr);
			    
			    AsmItemArray.append(ThisAsmItem);
			}
		    return (AsmItemArray);
		}
	    
	    catch (Exception e)
		{
		System.out.println("1: " + e);
		}
	    
	    return (null);
	    
	}
	

	//***********************************************************
	    /* Use feature Ids to append Assembly Item array 
	     */
	    public static AssemblyItems OTKUtilPopulateAssemblyItemsFeatureArray(
								     AssemblyItems AsmItemArray,		
								     WAssembly WAsm,
								     Model ItemOwner, 
								     intseq FeatIds, 
								     WComponentPath ItemCompPath,
								     BufferedWriter fp_out_asmitem)
	{	
	    try
		{
		    for (int i=0 ; i<FeatIds.getarraysize() ; i++)
			{
			    AssemblyItemInstructions ThisAsmInstr = 
				wfcComponentFeat.AssemblyItemInstructions_Create(ItemOwner, ModelItemType.ITEM_FEATURE, FeatIds.get(i) );
	  
			    if (ItemCompPath != null)
				ThisAsmInstr.SetItemCompPath(ItemCompPath);
			    
			    AssemblyItem ThisAsmItem = WAsm.CreateAssemblyItem(ThisAsmInstr);
			    
			    AsmItemArray.append(ThisAsmItem);
			}
		    return (AsmItemArray);
		}
	    
	    catch (Exception e)
		{
		    System.out.println("1: " + e);
		}
	    
	    return (null);
	    
	}
	
	//***********************************************************
		/*  Check component flexibility & set as flexible using 
		    provided Assembly Item array 
		*/
		public static WComponentFeat OTKUtilSetFlexibleComponent(Solid OwnerSolid, 
									 int FeatId, AssemblyItems AsmItemArray, BufferedWriter fp_out_asmitem)
	{
	    ComponentFeat CFeat = null;
	    WComponentFeat WCFeat = null ;
	    
	    try 
		{
		    Features Feats = OwnerSolid.ListFeaturesByType(Boolean.TRUE, FeatureType.FEATTYPE_COMPONENT);
		    
		    for (int i=0 ; i<Feats.getarraysize() ; i++)
			{
			    if (Feats.get(i).GetId() == FeatId) 
				{
				    fp_out_asmitem.write("For Feature ID: " + Feats.get(i).GetId());
				    fp_out_asmitem.newLine();
				    fp_out_asmitem.write("With Feature Number: " + Feats.get(i).GetNumber() );
				    fp_out_asmitem.newLine();
				    
				    CFeat = (ComponentFeat)(Feats.get(i));				
				    WCFeat = (WComponentFeat)(CFeat);
				}
			}
		    
		    boolean is_flex = WCFeat.IsFlexible();
		    
		    fp_out_asmitem.write(" Is Component Already Flexible ? : " + (is_flex ? "YES" : "NO") );
		    fp_out_asmitem.newLine();
		    
		    if (!is_flex)
			{
			    fp_out_asmitem.write("Making Component Flexible..." ); 
			    fp_out_asmitem.newLine();
			    
			    WCFeat.SetAsFlexible(AsmItemArray);
			    
			    is_flex = WCFeat.IsFlexible();
			    
			    fp_out_asmitem.write(" Is Component Flexible ? : " + (is_flex ? "YES" : "NO") );
			    fp_out_asmitem.newLine();
			    
			    
			}
		    return (WCFeat);
		}
	    
	    catch (Exception e)
		{
		    System.out.println("1: " + e);
		}
  
	    return (null);
	    
	}
	
	//***********************************************************
		    /*  Create flexible model using provided Assembly Item array 
		     */
		    public static Model OTKUtilCreateFlexibleModel (Solid OwnerSolid, 
								    int FeatId, WComponentPath WAsmCompPath, 
								    AssemblyItems AsmItemArray, BufferedWriter fp_out_asmitem)
	{
	    Model FlexModel = null;
	    
	    try 
		{
		    WComponentFeat WCFeat = OTKUtilSetFlexibleComponent(OwnerSolid,
									FeatId, AsmItemArray, fp_out_asmitem);
		    
		    fp_out_asmitem.write("Creating Flexible Model..." ); 
		    fp_out_asmitem.newLine();
      
		    FlexModel = WCFeat.CreateFlexibleModel(WAsmCompPath);
		    return (FlexModel);
		}
	    
	    catch (Exception e)
		{
		    System.out.println("1: " + e);
		}
	    
	    return (null);
	}
	

	//***********************************************************
			/*  Check component flexibility and create predefined 
			    flexibility component using provided Assembly Item array 
			*/
			public static void OTKUtilCreatePredefinedFlexibilityComponent(
										       Solid OwnerSolid, 
										       int FeatId, AssemblyItems AsmItemArray, 
										       Model ItemOwner, 
										       BufferedWriter fp_out_asmitem )
	{
	    try {
		
		Features Feats = OwnerSolid.ListFeaturesByType(Boolean.TRUE, FeatureType.FEATTYPE_COMPONENT);
		
		ComponentFeat CFeat = null;
		WComponentFeat WCFeat = null;
		
		for (int i=0 ; i<Feats.getarraysize() ; i++)
		    {
			if (Feats.get(i).GetId() == FeatId) 
			    {
				fp_out_asmitem.write("For Feature ID: " + Feats.get(i).GetId() );
				fp_out_asmitem.newLine();
				fp_out_asmitem.write("With Feature Number: " + Feats.get(i).GetNumber() );
				fp_out_asmitem.newLine();
				
				CFeat = (ComponentFeat)(Feats.get(i));				
				WCFeat = (WComponentFeat)(CFeat);
			    }
		    }
		
		boolean is_flex = WCFeat.IsFlexible();
		
		fp_out_asmitem.write(" Is Component Already Flexible ? : " + (is_flex ? "YES" : "NO") );
		fp_out_asmitem.newLine();
		
		if (!is_flex)
		    {
			fp_out_asmitem.write("Creating Predefined Flexibility Component..." ); 
			fp_out_asmitem.newLine();
			
			WCFeat.CreatePredefinedFlexibilityComponent(ItemOwner, AsmItemArray);
			
			is_flex = WCFeat.IsFlexible();
			
			fp_out_asmitem.write(" Is Component Flexible ? : " + (is_flex ? "YES" : "NO") );
			fp_out_asmitem.newLine();
		    }	    
	    }
	    
	    catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	    return;
	}
	
	
	
	};

//***********************************************************




